﻿// mod_raster.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"
#include "mod_raster.h"

#include "zModule.h"



IRasterLayerPtr OpenRasterLayer(const char* strPath);
IFeatureClassPtr OpenFeatureClass(const char* strPath);


// 这是导出变量的一个示例
MODRASTER_API int nmodraster=0;

// 这是导出函数的一个示例。
MODRASTER_API int fnmodraster(void)
{
    return 42;
}

// 这是已导出类的构造函数。
Cmodraster::Cmodraster()
{
    return;
}


//////////////////////////////////////////////////////////////////////////
IRasterLayerPtr OpenRasterLayer(const char* strPath);
IFeatureClassPtr OpenFeatureClass(const char* strPath);

int RasterClip(const char* szRasterPath, const char* szFeaturePath)
{
	HRESULT hr = S_OK;

	IRasterLayerPtr pRasterLayer = OpenRasterLayer(szRasterPath);
	if (pRasterLayer == NULL)
	{
		return -1;
	}

	IRasterPtr pRaster = NULL;;
	hr = pRasterLayer->get_Raster(&pRaster);
	if (FAILED(hr))
	{
		pRasterLayer->Release();
		return -1;
	}

	IFeatureClassPtr pFeatureClass = OpenFeatureClass(szFeaturePath);
	IGeoDatasetPtr pGeoDataset = NULL;
	hr = pFeatureClass->QueryInterface(IID_IGeoDataset, (LPVOID *)&pGeoDataset);
	if (FAILED(hr))
	{
		pFeatureClass->Release();
		pRasterLayer->Release();
		return -1;
	}

	IEnvelopePtr pExtent;
	hr = pGeoDataset->get_Extent(&pExtent);
	if (FAILED(hr))
	{
		pGeoDataset->Release();
		pFeatureClass->Release();
		pRasterLayer->Release();
		return -1;
	}

	//////////////////////////////////////////////////////////////////////////
	IGeoDatasetPtr pRasterDataset;
	hr = pRaster->QueryInterface(IID_IGeoDataset, (LPVOID*)&pRasterDataset);
	if (FAILED(hr))
	{
		pRaster->Release();
		pExtent->Release();
		pGeoDataset->Release();
		pFeatureClass->Release();
		pRasterLayer->Release();
		return FALSE;
	}

	IExtractionOpPtr pExtractionOp;
	hr = pExtractionOp.CreateInstance(CLSID_RasterExtractionOp);
	if (FAILED(hr))
	{
		pRaster->Release();
		pExtent->Release();
		pGeoDataset->Release();
		pFeatureClass->Release();
		pRasterLayer->Release();
		return FALSE;
	}

	IGeoDatasetPtr pOutRasterDataset = NULL;
	//hr = pOutRasterDataset.CreateInstance(CLSID_Raster);
	//if (FAILED(hr))
	//{
	//	return FALSE;
	//}
	hr = pExtractionOp->Rectangle(pRasterDataset, pExtent, VARIANT_TRUE, &pOutRasterDataset);
	if (FAILED(hr))
	{
		pRasterDataset->Release();
		pRaster->Release();
		pExtractionOp->Release();
		pExtent->Release();
		pGeoDataset->Release();
		pFeatureClass->Release();
		pRasterLayer->Release();
		return FALSE;
	}

	//////////////////////////////////////////////////////////////////////////
	// 处理pOutRasterDataset

	//////////////////////////////////////////////////////////////////////////

	pRasterDataset->Release();
	pRaster->Release();
	pExtractionOp->Release();
	pExtent->Release();
	pGeoDataset->Release();
	pFeatureClass->Release();
	pRasterLayer->Release();

	return 0;
}


IRasterLayerPtr OpenRasterLayer(const char* strPath)
{
	char szDrv[_MAX_DRIVE];
	char szDir[_MAX_DIR];
	char szName[_MAX_FNAME];
	char szExt[_MAX_EXT];
	char szFName[_MAX_FNAME];
	char szFolder[_MAX_PATH];
	_splitpath(strPath, szDrv, szDir, szName, szExt);
	_makepath(szFolder, szDrv, szDir, NULL, NULL);
	_makepath(szFName, NULL, NULL, szName, szExt);

	IWorkspaceFactoryPtr pWorksFactory;
	HRESULT hr = pWorksFactory.CreateInstance(CLSID_RasterWorkspaceFactory);
	if (FAILED(hr))
	{
		return NULL;
	}

	IWorkspacePtr pWorkspace;
	hr = pWorksFactory->OpenFromFile(CComBSTR(szFolder), 0, &pWorkspace);
	if (FAILED(hr))
	{
		return NULL;
	}

	IRasterWorkspacePtr pRasterWorkspace(pWorkspace);
	IRasterDatasetPtr pRasterDataset = NULL;
	hr = pRasterWorkspace->OpenRasterDataset(CComBSTR(szFName), &pRasterDataset);
	if (FAILED(hr))
	{
		return NULL;
	}

	IRasterLayerPtr pRasterLayer(CLSID_RasterLayer);
	pRasterLayer->CreateFromDataset(pRasterDataset);

	return pRasterLayer;
}


IFeatureClassPtr OpenFeatureClass(const char* strPath)
{
	char szDrv[_MAX_DRIVE];
	char szDir[_MAX_DIR];
	char szName[_MAX_FNAME];
	char szExt[_MAX_EXT];
	char szFName[_MAX_FNAME];
	char szFolder[_MAX_PATH];
	_splitpath(strPath, szDrv, szDir, szName, szExt);
	_makepath(szFolder, szDrv, szDir, NULL, NULL);
	_makepath(szFName, NULL, NULL, szName, szExt);

	IWorkspaceFactoryPtr pWorksFactory(CLSID_ShapefileWorkspaceFactory);
	if (!pWorksFactory)
	{
		return NULL;
	}

	IWorkspacePtr pWorkspace;

	HRESULT hr = pWorksFactory->OpenFromFile(CComBSTR(szFolder), 0, &pWorkspace);
	if (FAILED(hr))
	{
		return NULL;
	}

	IFeatureWorkspacePtr pFeatureWorkspace(pWorkspace);
	IFeatureClassPtr pFeaureClass;

	hr = pFeatureWorkspace->OpenFeatureClass(CComBSTR(szFName), &pFeaureClass);
	if (FAILED(hr))
	{
		return NULL;
	}

	return pFeaureClass;
}